//请求相关类

import axios from 'axios'
import qs from 'querystring'
// 生产
let api = ''
let api1 = ''
if (process.env.NODE_ENV == 'development') {
  console.log('开发模式')
  api = '/apis/api'
  api1 = '/jgpm/api'
  // api = '8012/api/api'
  // api1 = '9080/api/api'
} else {
  api = '/api'
  api1 = '/api'
}
const service1 = axios.create({
	baseURL: 'http://39.108.81.42:9080',//打包后的域名
});
// get请求封装
export default {
  fetchGet(url, params = {}, contentType = 'application/json;charset=UTF-8') {
    return new Promise((resolve, reject) => {
      axios.defaults.headers = {
        'Authorization': 'Bearer ' + localStorage.getItem('token'),
        'Content-Type': contentType
      }
      // get 参数不拼接会报404
      axios.get(`${api}/${url}?${qs.stringify(params)}`).then(res => {
        // console.log(res)
        resolve(res.data)
      }).catch(error => {
        reject(error.response.status)
      })
    })
  },
  /**智慧工地 未设置ngnix*/
  fetchPost1(url) {
    service1.defaults.headers = {
      'Authorization': 'Bearer ' + localStorage.getItem('token'),
      'Content-Type': 'application/json'
    }
    return new Promise((resolve, reject) => {
      service1.post(`${api1}/${url}`).then(res => {
        resolve(res.data)
      }).catch(error => {
        reject(error.response.status)
      })
    })
  },
   /**智慧工地 设置ngnix*/
  //  fetchPost1(url) {
  //   axios.defaults.headers = {
  //     'Authorization': 'Bearer ' + localStorage.getItem('token'),
  //     'Content-Type': 'application/json'
  //   }
  //   return new Promise((resolve, reject) => {
  //     axios.post(`${api}/${url}`).then(res => {
  //       resolve(res.data)
  //     }).catch(error => {
  //       reject(error.response.status)
  //     })
  //   })
  // },

  fetchPost(url, params = {},) {
    return new Promise((resolve, reject) => {
      axios.defaults.headers = {
        'Authorization': 'Bearer ' + localStorage.getItem('token'),
        'Content-Type': 'application/json'
      }
      axios.post(`${api}/${url}`, JSON.stringify(params)).then(res => {
        resolve(res.data)
      }).catch(error => {
        reject(error.response.status)
      })
    })
  },

  // post请求，get传参方式
  fetchPostGet(url, params = {}) {
    return new Promise((resolve, reject) => {
      axios.defaults.headers = {
        'Authorization': 'Bearer ' + localStorage.getItem('token'),
        'Content-Type': 'application/json'
      }
      axios.post(`${api}/${url}?${qs.stringify(params)}`).then(res => {
        resolve(res.data)
      }).catch(error => {
        reject(error.response.status)
      })
    })
  },
  /**
   * post和get混合传参 减少后台类
   * @param {*} url
   * @param {*} getParams get后的参数
   * @param {*} postParams Post的data对象
   */
  fetchPostAndGet(url, getParams = {}, postParams = {}) {
    return new Promise((resolve, reject) => {
      axios.defaults.headers = {
        'Authorization': 'Bearer ' + localStorage.getItem('token'),
        'Content-Type': 'application/json'
      }
      axios.post(`${api}/${url}?${qs.stringify(getParams)}`, JSON.stringify(postParams)).then(res => {
        resolve(res.data)
      }).catch(error => {
        reject(error.response.status)
      })
    })
  },
  // formData对象方式上传文件 提交文件不能写JSON.stringify(params)，应该直接提交params对象
  formDataUpLoad(url, params = []) {
    let formData = new FormData();
    params.forEach(i => {
      formData.append(i.key, i.value)
    })
    return new Promise((resolve, reject) => {
      axios.defaults.headers = {
        'Authorization': 'Bearer ' + localStorage.getItem('token'),
        'Content-Type': 'multipart/form-data'
      }
      axios.post(`${api}/${url}`, formData).then(res => {
        resolve(res.data)
      }).catch(error => {
        reject(error.response.status)
      })
    })
  }
}

